﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LDSP.BRL;
using System.Data;
using BYFW.DAL;
using LDSP.Util;
using LDSP.Helper;

namespace LDSP.UI.Admin
{
    public partial class MngDownload : LDSP.BRL.BaseSysAdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (this.RB())
                {
                    this.InitDataDynamicBefore();
                    if (!IsPostBack)
                    {
                        this.InitData();
                        this.BindGrid();
                    }
                    this.InitDataDynamicAfter();
                }
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        /// <summary>
        /// 传入参数，设置过滤条件
        /// </summary>
        protected void SetSpecialPageInfo()
        {
            g_pageInfo.Direction = false;
            g_pageInfo.Sort = "downloadDT";
            g_pageInfo.PageSize = GetPageSizeSetting();
            g_pageInfo.PageIndex = this.grd_list.PageIndex;
        }
        protected bool RB()
        {
            return this.AllowSystemUser();
        }
        protected void InitData()
        {
            //不缓存则清空页面信息
            if (!needCachePageInfo)
            {
                g_pageInfo.ClearPageInfo();
            }
            this.SetSpecialPageInfo();

            this.BindDDLPageSize();
            this.BindDDLDocType();
            this.BindDDLEnterprise();
        }
        protected void InitDataDynamicBefore()
        {
        }
        protected void InitDataDynamicAfter()
        {
            this.JSSetDateTimeReadOnly(this.txt_startDT.ClientID);
            this.JSSetDateTimeReadOnly(this.txt_endDT.ClientID);
        }
        protected void BindGrid()
        {
            this.grd_list.PageSize = g_pageInfo.PageSize;
            this.grd_list.PageIndex = g_pageInfo.PageIndex;
            DataTable dt = BYFW.SFL.CommonOP.SortedTable(new DownloadLog().GetRecords(g_pageInfo.ConditionWhere), g_pageInfo.GetSortExpression());
            this.grd_list.DataSource = dt;
            this.grd_list.DataBind();

            SavePageSizeSetting();

            this.lbl_times.Text = dt.Rows.Count.ToString();
        }
        protected void BindDDLEnterprise()
        {
            DataTable dt = new Enterprise().GetAll();
            this.ddl_enterprise.Items.Clear();
            this.ddl_enterprise.Items.Add(new ListItem("--请选择企业--", "0"));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ddl_enterprise.Items.Add(new ListItem(dt.Rows[i]["code"].ToString(), dt.Rows[i]["id"].ToString()));
            }
            try
            {
                this.ddl_enterprise.SelectedIndex = 0;
            }
            catch { }
        }
        protected void BindDDLDocType()
        {
            this.ddl_docType.Items.Clear();
            this.ddl_docType.Items.Add(new ListItem("--请选择文档类型--", "-1"));
            this.ddl_docType.Items.Add(new ListItem(DocType.法律法规.ToString(), ((int)DocType.法律法规).ToString()));
            this.ddl_docType.Items.Add(new ListItem(DocType.标准.ToString(), ((int)DocType.标准).ToString()));
            try
            {
                this.ddl_docType.SelectedIndex = 0;
            }
            catch { }
        }
        protected void BindDDLPageSize()
        {
            CommonControlInitial.InitialPageSize(this.ddl_pageSize);
            this.ddl_pageSize.SelectedValue = g_pageInfo.PageSize.ToString();
        }
        protected void GetCondition()
        {
            string cond = "";
            if (this.ddl_enterprise.SelectedIndex > 0)
            {
                cond = this.AppendCondition(cond, DefaultDB.GetSQLNormalCondition("enterpriseID", EDataType.Int, this.ddl_enterprise.SelectedValue, "=", true), true);
            }
            if (this.ddl_docType.SelectedIndex > 0)
            {
                cond = this.AppendCondition(cond, DefaultDB.GetSQLNormalCondition("docType", EDataType.Int, this.ddl_docType.SelectedValue, "=", true), true);
            }
            if (this.txt_name.Text.Trim() != "")
            {
                cond = this.AppendCondition(cond, DefaultDB.GetSQLMultiCondition("docName", EDataType.String, this.txt_name.Text, true, true), true);
            }
            if (this.txt_startDT.Text.Trim() != "")
            {
                cond = this.AppendCondition(cond, DefaultDB.GetSQLNormalCondition("downloadDT", EDataType.Date, this.txt_startDT.Text, ">=", true), true);
            }
            if (this.txt_endDT.Text.Trim() != "")
            {
                cond = this.AppendCondition(cond, DefaultDB.GetSQLNormalCondition("downloadDT", EDataType.Date, this.txt_endDT.Text, "<=", true), true);
            }
            g_pageInfo.Condition = cond;
        }

        protected void btn_search_Click(object sender, EventArgs e)
        {
            try
            {
                this.GetCondition();
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }

        protected void grd_list_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {

            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void grd_list_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    DownloadLog ett = new DownloadLog();
                    ett.GetProperties(((DataRowView)e.Row.DataItem).Row);
                    this.ShowSequenceNumber(e.Row, 1);
                    e.Row.Cells[2].Text = ett.DocName;
                    e.Row.Cells[3].Text = ett.EnterpriseCode;
                    e.Row.Cells[5].Text = ett.DocType.ToString();
                    e.Row.Cells[6].Text = this.FormatDT(ett.DownloadDT);
                }
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void grd_list_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                g_pageInfo.PageIndex = e.NewPageIndex;
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void grd_list_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                if (g_pageInfo.Sort == e.SortExpression)
                {
                    g_pageInfo.Direction = !g_pageInfo.Direction;
                }
                else
                {
                    g_pageInfo.Direction = true;
                }
                g_pageInfo.Sort = e.SortExpression;
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void ddl_pageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                g_pageInfo.PageSize = this.GetInt(this.ddl_pageSize.SelectedValue);
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void btn_Export_All_Click(object sender, EventArgs e)
        {
            //Collect codifications.
            DataTable source = BYFW.SFL.CommonOP.SortedTable(new DownloadLog().GetRecords(string.Empty), g_pageInfo.GetSortExpression());

            //Export.
            DownloadHistoryExport.Export(Server, source);
        }
    }

}